<!-- 提交反馈 -->
<template>
	<view>
		<!-- 问题和意见 -->
		<view class="title">
			<view class="title_item">
				<view class="dian"></view>
				<view class="title_text">
					问题和意见
				</view>
			</view>

			<!-- <view class="title_num">
				<text>0</text>
				<text>/500</text>
			</view> -->
		</view>
		<view class="" style="padding: 20rpx;">
			<u--textarea v-model="opinion" height="100" maxlength="500" placeholder="请填写详细的问题描述以便我们提供更好的帮助" count>
			</u--textarea>
		</view>

		<!-- 图片上传 -->
		<view class="title">
			<view class="title_item">
				<view class="dian"></view>
				<view class="title_text">
					图片（选填，提供问题截图）
				</view>
			</view>
		</view>
		<view class="" style="margin-top: 20rpx;margin-left: 20rpx;">
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" :multiple="true"
				:maxCount="9" capture="album"></u-upload>
		</view>

		<!-- 联系电话 -->
		<view class="title">
			<view class="title_item">
				<view class="dian"></view>
				<view class="title_text">
					联系电话
				</view>
			</view>
		</view>
		<view class="" style="padding: 20rpx;">
			<u--input v-model="phone_number" type="number" placeholder="选填，便于我们与你联系">
			</u--input>
		</view>

		<!-- 提交按钮 -->
		<view class="bottom">
			<view class="bottom_btn" @click="submit()">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				opinion: '', //反馈内容
				fileList1: [], //显示的图片数组
				qiniu_token: '',
				imgList: [], //上传的图片数组
				phone_number: '',

				token: '',
				member_id: '',
				company_id: '',
				company_name: ''
			}
		},
		onLoad() {
			this.token = uni.getStorageSync('btlh_process_token') || '';
			this.member_id = uni.getStorageSync('btlh_process_member_id') || '';
			this.company_id = uni.getStorageSync('bthl_process_LoginCompanyId') || '';
			this.company_name = uni.getStorageSync('bthl_process_LoginCompanyName') || '';


		},
		onShow() {
			this.getQiniuToken();
		},
		methods: {
			// 提交反馈
			async submit() {
				if (this.opinion == '') {
					return uni.showToast({
						title: '请填写详细的反馈内容',
						icon: 'none'
					});
				}
				
				const {
					data: res
				} = await this.$myRequest({
					url: 'Help/set_problem',
					method: 'POST',
					data: {
						member_id: this.member_id,
						token: this.token,
						content: this.opinion,
						annex: JSON.stringify(this.imgList),
						phone: this.phone_number
					}
				});

				console.log(res)
				if (res.code !== 200) {
					return uni.showToast({
						title: res.datas.msg,
						icon: 'none'
					});
				}
				if (res.code == 200) {
					uni.showToast({
						title: res.datas.msg,
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)

				}
			},
			// 获取七牛云token
			async getQiniuToken() {
				const {
					data: res
				} = await this.$myRequest({
					url: 'Qiniu/qiniuToken',
					method: 'GET',
					data: {
						member_id: this.member_id,
						token: this.token,
					}
				});
				this.qiniu_token = res;
				console.log(res)
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.imgList.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log('url--------' + url)
				let a = url.lastIndexOf('.');
				let b = url.slice(0, a);
				let c = url.slice(a);

				let timestamp = Date.parse(new Date());
				timestamp = timestamp / 1000;
				console.log("当前时间戳为：" + timestamp);
				let randomNum = (Math.random() * 1000).toFixed(0);
				console.log("当前随机数：" + randomNum);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://up-z2.qiniup.com',
						filePath: url,
						name: 'file',
						formData: {
							token: this.qiniu_token,
							key: 'control/mobile/' + b + timestamp + randomNum + c
						},
						success: (res) => {
							resolve(res.data.data)
							console.log(res)
							const result = JSON.parse(res.data).key;
							let imgurl = 'https://qiniu.sxpz.vip/' + result;
							this.imgList.push(imgurl);
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bottom {
		display: flex;
		flex-direction: row;
		justify-content: center;
		margin-top: 80rpx;

		.bottom_btn {
			width: 320rpx;
			height: 70rpx;
			background: #40A0F2;
			border-radius: 8rpx;
			color: #FFFFFF;
			font-size: 30rpx;
			line-height: 70rpx;
			text-align: center;
		}
	}


	.title {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;

		margin-top: 20rpx;
		margin-left: 20rpx;

		.title_item {
			display: flex;
			flex-direction: row;
			align-items: center;

			.title_text {
				font-size: 30rpx;
				color: #333333;
				margin-left: 10rpx;
			}

			.dian {
				width: 8rpx;
				height: 8rpx;
				background: #333333;
				border-radius: 50%;
			}
		}

		.title_num {
			margin-right: 20rpx;
			font-size: 24rpx;
			color: #666666;
		}

	}
</style>
